<template>
  <div class="home">
<!-- 

  名门望族 张琼宇  首页页面

 -->
    <el-container>
      <el-header>
        <span class="span">
          <img src="../assets/download.png" alt="">
          后台电商管理
        </span>
        <el-button type="primary" @click="out" class="button">退出</el-button>
      </el-header>
      <el-container>
        <el-aside :width="flag?'65px':'200px'">
          <div @click="flag=!flag" style="text-align: center;">|||</div>
          <el-menu :default-active="active.path" class="el-menu-vertical-demo" background-color="#545c64" text-color="#fff" active-text-color="#ffd04b" unique-opened :collapse-transition="false" :collapse="flag" router>
            <el-submenu :index="item.id+''" v-for="item in list" :key="item.id">
              <template slot="title">
                <i class="el-icon-location"></i>
                <span>{{item.authName}}</span>
              </template>
              <el-menu-item :index="it.path" v-for="it in item.children" :key="it.id" @click="savename(item.authName,it.authName,it.path)">{{it.path}}--{{it.authName}}</el-menu-item>
            </el-submenu>
          </el-menu>
        </el-aside>
        <el-main>
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>

  </div>
</template>

<script>
import { List } from '@/utils/homeapi'
export default {
  data() {
    return {
      list: [],
      flag: false,
      active: JSON.parse(sessionStorage.getItem('menu')) || {}
    }
  },
  methods: {
    async getlist() {
      let res = await List()
      this.list = res.data // 侧边栏数据
        // console.log(this.list);
      var route = [] // 定义一个空的数组
      this.list.forEach(item => { // 循环list 侧边栏
        item.children.forEach(it => { // 双层循环 里面的children
          var obj = {      // 定义一个数组 存 path 路径 
            path: '/' + it.path,  // 找到第一个0号元素变为大写然后拼接后面的数组
            name: it.path.charAt(0).toUpperCase() + it.path.slice(1)
          }
          route.push(obj)  // 将组好的数组 加到 vuex中的路径中
        })
      })
      // 判断vuex中的modeules模块上的obj是否为空 如果为空继续执行 如果不为空将obj合并到route中
      if (this.$store.state.modeules.obj.path) {
        route.push(this.$store.state.modeules.obj)
      }

      // 将route存入vuex中
      this.$store.commit('modeules/addRoute', route)
      //   console.log(this.$store.commit('modeules/addRoute', route));
    },
    out() { // 退出
      sessionStorage.removeItem('token') // 删除本地中的token
      this.$store.commit('modeules/addRoute', []) // 让vuex模块中addRoute 侧边内容清空
      this.$store.commit('modeules/addOneRoute', {}) // 让vuex模块中addOneRoute 侧边内容清空
      this.$router.push('/login')  // 跳转到login页面
    },
    savename(onename, towname, path) {
    //   console.log(onename, towname, path);
        var obj = {onename, towname, path}
        sessionStorage.setItem('menu',JSON.stringify(obj))
    }
  },
  mounted() {
    this.getlist()
  },
}
</script>

<style lang="scss" scoped>
.home {
  height: 100%;
  min-width: 1261px;
  .el-container {
    height: 100%;
    img{
      width: 20px;
    }
  }
}

.el-header {
  background-color: #b3c0d1;
  color: #333;
  display: flex;
  align-items: center;
  justify-content: space-between;
  line-height: 60px;
}

.el-aside {
  background-color: #d3dce6;
  color: #333;
}

.el-main {
  background-color: #e9eef3;
  color: #333;
}

body > .el-container {
  margin-bottom: 40px;
}

.el-container:nth-child(5) .el-aside,
.el-container:nth-child(6) .el-aside {
  line-height: 260px;
}

.el-container:nth-child(7) .el-aside {
  line-height: 320px;
}
.span{
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: 160px;
  height: 100%;
  img{
    flex: 1;
    width: 61px;
    height: 56px;
  }
}

</style>